package org.metawatch.manager;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;


public class _4911MusicplaybackserviceAdapter{
	

	private Context currentContext;
	private String currentTrack = null;
	private String currentArtist = null;
	private String currentAlbum = null;
	
//	private long totalTime = 0;
//	private long currentTime = 0;
//	private long remainingTime = 0;
	
	/**
	 * Set up a connection to the musicplaybackservice
	 * @param c the current context just get it by doing: Context currentContext = getApplicationContext();
	 */
	public _4911MusicplaybackserviceAdapter(Context c){
		currentContext = c;
		IntentFilter intFilter = new IntentFilter();
		intFilter.addAction("com.android.music.playstatechanged");
		intFilter.addAction("com.android.music.metachanged");
		intFilter.addAction("com.android.music.queuechanged");
		intFilter.addAction("com.android.music.playbackcomplete");
		intFilter.addAction("com.android.music.asyncopencomplete");
		
		BroadcastReceiver intRcv = new musicBroadcastRcv();
		c.registerReceiver(intRcv, intFilter);
	}
	
	/**
	 * Fast forward to the next track
	 */
	public void nextTrack(){
		currentContext.sendBroadcast(new Intent("com.android.music.musicservicecommand.next"));
	}
	
	/**
	 * Go back to the previous track
	 */
	public void prevTrack(){
		currentContext.sendBroadcast(new Intent("com.android.music.musicservicecommand.previous"));
	}
	
	/**
	 * Switch between play and pause
	 */
	public void togglePlayPause(){
		currentContext.sendBroadcast(new Intent("com.android.music.musicservicecommand.togglepause"));
	}
	
	/**
	 * Get the current track's name
	 * @return name of current playing track
	 */
	public String getTrack(){
		return currentTrack;
	}
	
	/**
	 * Get the current track's artist
	 * @return artist of current playing track
	 */
	public String getArtist(){
		return currentArtist;
	}
	
	/**
	 * Get the current track's album title
	 * @return album title of current playing track
	 */
	public String getAlbum(){
		return currentAlbum;
	}

	
//	
//	public String getCurrentTime(){
//		String answer = null;
//		try {
//			genCurrentTime();
//			answer = Long.toString(currentTime);
//		} catch (RemoteException e) {
//			// TODO Auto-generated catch block
//			answer = null;
//		}
//		return answer;
//	}
	
	/**
	 * Private class used to handle incoming messages from the music playback service
	 * @author MetaWatch-Team32
	 *
	 */
	private class musicBroadcastRcv extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			
			// Update information to watch
			
			currentTrack = intent.getStringExtra("track");
			currentArtist = intent.getStringExtra("artist");
			currentAlbum = intent.getStringExtra("album");
			
			MetaWatchService.updateSong(currentTrack, currentArtist, currentAlbum);
		}
		
	}
}
